package cn.iocoder.yudao.module.crossborder.platform.service.platformapiconfig;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformapiconfig.vo.PlatformApiConfigPageReqVO;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformapiconfig.vo.PlatformApiConfigSaveReqVO;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformapiconfig.PlatformApiConfigDO;
import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformapiconfig.PlatformApiConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.PLATFORM_API_CONFIG_NOT_EXISTS;

/**
 * 跨境电商平台API配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformApiConfigServiceImpl implements PlatformApiConfigService {

    @Resource
    private PlatformApiConfigMapper platformApiConfigMapper;

    @Override
    public Long createPlatformApiConfig(PlatformApiConfigSaveReqVO createReqVO) {
        // 插入
        PlatformApiConfigDO platformApiConfig = BeanUtils.toBean(createReqVO, PlatformApiConfigDO.class);
        platformApiConfigMapper.insert(platformApiConfig);

        // 返回
        return platformApiConfig.getId();
    }

    @Override
    public void updatePlatformApiConfig(PlatformApiConfigSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformApiConfigExists(updateReqVO.getId());
        // 更新
        PlatformApiConfigDO updateObj = BeanUtils.toBean(updateReqVO, PlatformApiConfigDO.class);
        platformApiConfigMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformApiConfig(Long id) {
        // 校验存在
        validatePlatformApiConfigExists(id);
        // 删除
        platformApiConfigMapper.deleteById(id);
    }

    @Override
        public void deletePlatformApiConfigListByIds(List<Long> ids) {
        // 删除
        platformApiConfigMapper.deleteByIds(ids);
        }


    private void validatePlatformApiConfigExists(Long id) {
        if (platformApiConfigMapper.selectById(id) == null) {
            throw exception(PLATFORM_API_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public PlatformApiConfigDO getPlatformApiConfig(Long id) {
        return platformApiConfigMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformApiConfigDO> getPlatformApiConfigPage(PlatformApiConfigPageReqVO pageReqVO) {
        return platformApiConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PlatformApiConfigDO> getPlatformApiConfigListByPlatformId(Long platformId) {
        return platformApiConfigMapper.selectListByPlatformId(platformId);
    }

}